import time
import threading

lock = threading.RLock()


def minus1():
    global num2
    lock.acquire()
    num2 -= 1
    lock.release()


def minus2():
    global num1  # 在每个线程中都获取这个全局变量
    lock.acquire()
    minus1()
    time.sleep(1)
    num1 -= 1  # 对此公共变量进行-1操作
    print('%s--get num1:%s,num2:%s' % (threading.current_thread().name, num1, num2))
    lock.release()


num1, num2 = 5, 9  # 设定共享变量
thread_list = []

if __name__ == '__main__':
    for i in range(5):
        t = threading.Thread(target=minus2)
        t.start()
        thread_list.append(t)

    while threading.active_count() != 1:
        print(threading.active_count())
        time.sleep(1)
    else:
        print('----all threads done---')
    print('final num:', num1, num2)
